home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 March / PCWorld_2007-03_cd.bin / v cisle / httrack / httrack-3.41-2.exe / {app} / libtest / callbacks-example-listlinks.c < prev    next >
C/C++ Source or Header  |  2006-06-04  |  5KB  |  131 lines

  1. /*
  2.     HTTrack external callbacks example
  3.     .c file
  4.  
  5.     How to build: (callback.so or callback.dll)
  6.       With GNU-GCC:
  7.         gcc -O -g3 -Wall -D_REENTRANT -shared -o mycallback.so callbacks-example.c -lhttrack1
  8.       With MS-Visual C++:
  9.         cl -LD -nologo -W3 -Zi -Zp4 -DWIN32 -Fe"mycallback.dll" callbacks-example.c libhttrack1.lib
  10.  
  11.       Note: the httrack library linker option is only necessary when using libhttrack's functions inside the callback
  12.  
  13.     How to use:
  14.       httrack --wrapper mycallback ..
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20.  
  21. /* Standard httrack module includes */
  22. #include "httrack-library.h"
  23. #include "htsopt.h"
  24. #include "htsdefines.h"
  25.  
  26. /* Function definitions */
  27. static int process_file(t_hts_callbackarg *carg, httrackp *opt, char* html, int len, const char* url_address, const char* url_file);
  28. static int check_detectedlink(t_hts_callbackarg *carg, httrackp *opt, char* link);
  29. static int check_loop(t_hts_callbackarg *carg, httrackp *opt, void* back,int back_max,int back_index,int lien_tot,int lien_ntot,int stat_time,void* stats);
  30. static int end(t_hts_callbackarg *carg, httrackp *opt);
  31.  
  32. /*
  33.   This sample just lists all links in documents with the parent link:
  34.   <parent> -> <link>
  35.   This sample can be improved, for example, to make a map of a website.
  36. */
  37.  
  38. typedef struct t_my_userdef {
  39.   char currentURLBeingParsed[2048];
  40. } t_my_userdef;
  41.  
  42. /* 
  43. module entry point 
  44. */
  45. EXTERNAL_FUNCTION int hts_plug(httrackp *opt, const char* argv) {
  46.   t_my_userdef *userdef;
  47.   /* */
  48.   const char *arg = strchr(argv, ',');
  49.   if (arg != NULL)
  50.     arg++;
  51.  
  52.   /* Create user-defined structure */
  53.   userdef = (t_my_userdef*) malloc(sizeof(t_my_userdef));    /* userdef */
  54.   userdef->currentURLBeingParsed[0] = '\0';
  55.  
  56.   /* Plug callback functions */
  57.   CHAIN_FUNCTION(opt, check_html, process_file, userdef);
  58.   CHAIN_FUNCTION(opt, end, end, userdef);
  59.   CHAIN_FUNCTION(opt, linkdetected, check_detectedlink, userdef);
  60.   CHAIN_FUNCTION(opt, loop, check_loop, userdef);
  61.  
  62.   return 1;  /* success */
  63. }
  64.  
  65. static int process_file(t_hts_callbackarg *carg, httrackp *opt, char* html, int len, const char* url_address, const char* url_file) {
  66.   t_my_userdef *userdef = (t_my_userdef*) CALLBACKARG_USERDEF(carg);
  67.   char * const currentURLBeingParsed = userdef->currentURLBeingParsed;
  68.  
  69.   /* Call parent functions if multiple callbacks are chained. */
  70.   if (CALLBACKARG_PREV_FUN(carg, check_html) != NULL) {
  71.     if (!CALLBACKARG_PREV_FUN(carg, check_html)(CALLBACKARG_PREV_CARG(carg), opt, html, len, url_address, url_file)) {
  72.       return 0;  /* Abort */
  73.     }
  74.   }
  75.  
  76.   /* Process */
  77.   printf("now parsing %s%s..\n", url_address, url_file);
  78.   strcpy(currentURLBeingParsed, url_address);
  79.   strcat(currentURLBeingParsed, url_file);
  80.  
  81.   return 1;  /* success */
  82. }
  83.  
  84. static int check_detectedlink(t_hts_callbackarg *carg, httrackp *opt, char* link) {
  85.   t_my_userdef *userdef = (t_my_userdef*) CALLBACKARG_USERDEF(carg);
  86.   char * const currentURLBeingParsed = userdef->currentURLBeingParsed;
  87.  
  88.   /* Call parent functions if multiple callbacks are chained. */
  89.   if (CALLBACKARG_PREV_FUN(carg, linkdetected) != NULL) {
  90.     if (!CALLBACKARG_PREV_FUN(carg, linkdetected)(CALLBACKARG_PREV_CARG(carg), opt, link)) {
  91.       return 0;  /* Abort */
  92.     }
  93.   }
  94.  
  95.   /* Process */
  96.   printf("[%s] -> [%s]\n", currentURLBeingParsed, link);
  97.  
  98.   return 1;  /* success */
  99. }
  100.  
  101. static int check_loop(t_hts_callbackarg *carg, httrackp *opt, void* back,int back_max,int back_index,int lien_tot,int lien_ntot,int stat_time,void* stats) {
  102.   static int fun_animation=0;
  103.  
  104.   /* Call parent functions if multiple callbacks are chained. */
  105.   if (CALLBACKARG_PREV_FUN(carg, loop) != NULL) {
  106.     if (!CALLBACKARG_PREV_FUN(carg, loop)(CALLBACKARG_PREV_CARG(carg), opt, back, back_max, back_index, lien_tot, lien_ntot, stat_time, stats)) {
  107.       return 0;  /* Abort */
  108.     }
  109.   }
  110.  
  111.   /* Process */
  112.   printf("%c\r", "/-\\|"[(fun_animation++)%4]);
  113.   return 1;
  114. }
  115.  
  116. static int end(t_hts_callbackarg *carg, httrackp *opt) {
  117.   t_my_userdef *userdef = (t_my_userdef*) CALLBACKARG_USERDEF(carg);
  118.   fprintf(stderr, "** info: wrapper_exit() called!\n");
  119.   if (userdef != NULL) {
  120.     free(userdef);
  121.     userdef = NULL;
  122.   }
  123.  
  124.   /* Call parent functions if multiple callbacks are chained. */
  125.   if (CALLBACKARG_PREV_FUN(carg, end) != NULL) {
  126.     return CALLBACKARG_PREV_FUN(carg, end)(CALLBACKARG_PREV_CARG(carg), opt);
  127.   }
  128.  
  129.   return 1;  /* success */
  130. }
  131.